package cn.com.platform.web.manage.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.easy.permission.service.system.urlreserve.anotion.UrlReserveAnotion;
import cn.com.easy.permission.web.controller.BaseController;
import cn.com.easy.permission.web.dto.MessageDTO;
import cn.com.easy.permission.web.dto.PageRequestParamDTO;
import cn.com.easy.utils.Page;
import cn.com.easy.utils.PageUtils;
import cn.com.easy.utils.ResponseOutputUtils;
import cn.com.platform.dao.BlockPageDao;
import cn.com.platform.entity.BlockPageEntity;
import cn.com.platform.mybatis.BlockMybatisDao;
import cn.com.platform.web.manage.dto.BlockDto;

import com.github.miemiedev.mybatis.paginator.domain.PageList;

/**
 * 推荐位管理
 * 
 * @author lmh 2016年9月22日
 * 
 */
@Controller
@RequestMapping("/manage/block")
public class BlockManageController extends BaseController {
	private Logger logger = LoggerFactory
			.getLogger(BlockManageController.class);

	/** 推荐位dao */
	@Autowired
	private BlockMybatisDao blockMybatisDao;
	/** */
	@Autowired
	private BlockPageDao blockPageDao;

	/**
	 * 推荐位管理
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @author lmh 2016年9月23日
	 */
	@UrlReserveAnotion("推荐位管理")
	@RequestMapping("")
	public String index(HttpServletRequest request, HttpServletResponse response) {
		return "/manage/block/block";
	}

	/**
	 * 获取所有推荐位
	 * 
	 * @param request
	 * @param response
	 * @param pageRequestParamDTO
	 * @author lmh 2016年9月23日
	 */
	@UrlReserveAnotion("获取所有推荐位")
	@RequestMapping("/get/all")
	public void getAllBlock(HttpServletRequest request,
			HttpServletResponse response,
			PageRequestParamDTO pageRequestParamDTO) {
		try {
			if (pageRequestParamDTO.getSortName() == null) {
				pageRequestParamDTO.setSortName("id");
				pageRequestParamDTO.setSortOrder("asc");
			}
			PageList<BlockDto> pageList = blockMybatisDao
					.findAll(pageRequestParamDTO.buildMyBatisPageRequest());
			Page<BlockDto> page = PageUtils.getPage(pageList);
			ResponseOutputUtils.renderJson(response,
					MessageDTO.newInstance("", true, page));
		} catch (Exception ex) {
			logger.error("获取所有推荐位异常.", ex);
			ResponseOutputUtils.renderJson(
					response,
					MessageDTO.newInstance("", false,
							"获取所有推荐位异常.ex:" + ex.getLocalizedMessage()));
		}
	}

	/**
	 * 获取推荐页面
	 * 
	 * @param request
	 * @param response
	 * @author wxy 2016年8月18日
	 */
	@UrlReserveAnotion("获取推荐页面")
	@RequestMapping(value = "/findBlockPage")
	public void getAllProvince(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			org.springframework.data.domain.Page<BlockPageEntity> blockPageEntities = blockPageDao
					.findByDeleteStatus(false, null);

			ResponseOutputUtils.renderJson(
					response,
					MessageDTO.newInstance("", true,
							blockPageEntities.getContent()));
		} catch (Exception ex) {
			logger.error("获取推荐页面异常.", ex);
			ResponseOutputUtils.renderJson(
					response,
					MessageDTO.newInstance("", false,
							"获取推荐页面异常.ex:" + ex.getLocalizedMessage()));
		}
	}
}
